import time
import base64

import pandas as pd
import requests
from lxml import etree


# TODO 在本模块内抓取天气数据
def get_token():
    now = str(int(time.time()*1000)).encode('utf-8')
    return base64.b64encode(now).decode('utf-8')


def get_one_page(city, page):
    url = f'http://127.0.0.1:5000/city_weather?city={city}&page={page}&TOKEN={get_token()}'
    headers = {
        'Referer': 'http://127.0.0.1:5000/',
        'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
        'Cookie': r'salt="\302\210D\303\2609\302\221\007\302\230\302\211f9\303\254J:U\027\303\205V\302\276\302\213\303\257\303\227\303\230\303\223\303\246\302\230*4"'
    }
    response = requests.get(url, headers=headers)
    tree = etree.HTML(response.text)
    rows = tree.xpath('//tr')
    header = tree.xpath('//tr/th//text()')
    rows = [row.xpath('./td//text()') for row in rows[1:]]
    res = pd.DataFrame(rows, columns=header)
    return res
    pass


def get_one_city(city):
    res = pd.DataFrame()
    for page in range(1, 36):
        print(page, end=' ')
        one_page = get_one_page(city, page)
        time.sleep(1.5)
        if len(one_page) == 0:
            res.to_csv(f'day_data/{city}_day.csv')
            return res
        res = pd.concat([res, one_page], axis=0)
    pass


if __name__ == '__main__':
    cits = ['大理']
    for city in cits:
        get_one_city(city)